<template>
  <div class="app-container">  
      <el-form ref="filterForm" :model="formData" :rules="rules" label-position="left" label-width="100px">
         <el-form-item label="学校名称" prop="schoolName">
          <el-input v-model="formData.schoolName" clearable placeholder="学校名称" maxlength="80" show-word-limit/>
        </el-form-item>
        <el-form-item label="学校介绍" prop="remark">
          <el-input type="textarea" v-model="formData.content" maxlength="5000" show-word-limit clearable placeholder="学校介绍"/>
        </el-form-item>
        <el-form-item label="学校性质" prop="schoolStyle">
            <el-radio-group v-model="formData.schoolStyle" >
              <el-radio-button label="1">公办</el-radio-button>
              <el-radio-button label="2">民办</el-radio-button>
            </el-radio-group>
        </el-form-item>
        <el-form-item label="学校类型" prop="quote">
            <el-radio-group v-model="formData.shoolShape" >
              <el-radio-button label="1">中职</el-radio-button>
              <el-radio-button label="2">普通高中</el-radio-button>
            </el-radio-group>
        </el-form-item>
        <el-form-item label="学校等级" prop="quote">
            <el-radio-group v-model="formData.schoolScale" >
              <el-radio-button label="1">重点</el-radio-button>
              <el-radio-button label="2">市级重点</el-radio-button>
              <el-radio-button label="3">省级重点</el-radio-button>
              <el-radio-button label="4">国家级重点</el-radio-button>
            </el-radio-group>
        </el-form-item>
        <el-form-item label="学校LOGO" prop="schoolLogo">
          <Upload v-model="formData.schoolLogo" />
        </el-form-item>
        <el-form-item label="所在区域" prop="schoolRegion">
          <Region v-model="formData.schoolRegion" />
        </el-form-item>
         <el-form-item label="学校地址" prop="schoolAddress">
          <el-input v-model="formData.schoolAddress" clearable placeholder="学校地址" maxlength="80" show-word-limit/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input type="textarea" v-model="formData.remark" maxlength="200" show-word-limit clearable placeholder="备注"/>
        </el-form-item>
      </el-form>
      <el-row :gutter="20">
        <el-col :span="14">          
            <el-button type="info" @click="onFormSaveButton()">保存</el-button>
            <el-button type="primary" @click="onFormSaveAndNewButton()">保存并新建</el-button>
            <el-button type="primary" @click="onFormNewButton()">新建</el-button>
        </el-col>
        <el-col :span="6" style="text-align:right;">          
          <el-button type="warning" @click="onFormCannelButton()">取消</el-button>
          <el-button type="danger" v-if="schoolID > 0" @click="onFormRemoveButton()">删除</el-button>
        </el-col>
      </el-row>

  </div>
</template>

<script>
import waves from '@/directive/waves' // waves directive
import Region from '@/components/Region/RegionSimple'
import Upload from '@/components/Upload/SingleImage3'
import {getSchoolDetail,saveSchool,removeSchool} from '@/api/knowledge/school'



export default {
  name: 'schoolForm',
  components: { Region,Upload },
  directives: { waves },
  filters: {
  },
  data() {
    return {
      schoolID:undefined,
      exchangeOptions:[],
      formData: {
      },
      rules:{
         schoolName: [
            { required: true, message: '请输入期货名称', trigger: 'blur' },
            { min: 2, max: 80, message: '长度在 3 到 80 个字符', trigger: 'blur' }
          ]
      }
    }
  },
  created() {
    let schoolID = this.$route.query.schoolID 
    schoolID = parseInt(schoolID)
    if(isNaN(schoolID) == false){
      this.schoolID = schoolID
      this.getInfo()
    }
  },
  methods: {
    getInfo:function(){
      let _this = this;
      getSchoolDetail({
        "schoolID":this.schoolID
      }).then((response) => {
        console.log("getList",response)
          if (response.code === 200) {
            this.formData = response.data
          } else {
            _this.$message({
              message: response.msg,
              type: 'error'
            })
          }
      })
    },
    onFormSaveButton:function(){
      let _this = this;
      this.$refs['filterForm'].validate((valid) => {
        if (valid) {
          if(_this.schoolID != undefined){
            _this.formData['schoolID'] = this.formData.ID
          }
            saveSchool(_this.formData).then((response) => {
              console.log("getList",response)
                if (response.code === 200) {
                  _this.$message({
                    message: "成功",
                    type: 'error'
                  })
                } else {
                  _this.$message({
                    message: response.msg,
                    type: 'error'
                  })
                }
            })
        } else {
            console.log('error submit!!');
            return false;
        }
      })
    },
    onFormSaveAndNewButton:function(){
      let _this = this;
        if(_this.schoolID != undefined){
          _this.formData['schoolID'] = this.formData.ID
        }
      saveSchool(_this.formData).then((response) => {
          if (response.code === 200) {
            _this.onFormNewButton()
          } else {
            _this.$message({
              message: response.msg,
              type: 'error'
            })
          }
      })
    },
    onFormNewButton:function(){
        this.formData = {

        }
    },
    onFormCannelButton:function(){
      this.$router.push({ path: 'school'})
    },
    onFormRemoveButton:function(){
      let _this = this;
      removeSchool({
        "schoolID":this.schoolID
      }).then((response) => {
        console.log("getList",response)
          if (response.code === 200) {
            _this.onFormCannelButton()
          } else {
            _this.$message({
              message: response.msg,
              type: 'error'
            })
          }
      })
    }
  }
}
</script>
<style>
</style>